<template>
  <div>
    <flexbox align="flex-start">
      <flexbox-item :span="1">
        <div :class="`avatar${((account[0]||'u').charCodeAt()+(account[1]||'n').charCodeAt()) % 6}`">{{ username[0] }}</div>
      </flexbox-item>
      <flexbox-item :span="11">
        <div class="info-box">
          <p class="username-label">{{ username }}</p>
          <p class="datetime-label" v-if="datetime">{{ datetime | dateFormat }}</p>
        </div>
      </flexbox-item>
    </flexbox>
  </div>
</template>

<style lang="less">
@color1: #f19494;
@color2: #8cb6f0;
@color3: #c4e0a6;
@color4: #8fe5db;
@color5: #b9a8df;
@color6: #dda8e0;

.avatar(@color) {
  width: 2rem;
  height: 2rem;
  line-height: 2rem;
  background-color: @color;
  color: white;
  border-radius: 1rem;
  text-align: center;
}

.avatar0 {
  .avatar(@color1);
}
.avatar1 {
  .avatar(@color2);
}
.avatar2 {
  .avatar(@color3);
}
.avatar3 {
  .avatar(@color4);
}
.avatar4 {
  .avatar(@color5);
}
.avatar5 {
  .avatar(@color6);
}

.info-box {
  margin-left: 0.5rem;
  font-size: 0.75rem;
  line-height: 1rem;
  .username-label {
    color: #2e4980;
    font-weight: 600;
  }
  .datetime-label {
    color: #666;
  }
}
</style>

<script>
import { Flexbox, FlexboxItem, dateFormat } from "vux";

export default {
  name: "Avatar",
  props: ["account", "username", "datetime"],
  components: {
    Flexbox,
    FlexboxItem
  },
  filters: {
    dateFormat: datetime =>
      dateFormat(new Date(datetime), "YYYY-MM-DD HH:mm:ss")
  }
};
</script>

